package com.geek.factory.controller;

import com.geek.factory.constant.SuccessConstant;
import com.geek.factory.dto.DeviceRepairLogDTO;
import com.geek.factory.entity.DeviceRepairLog;
import com.geek.factory.result.Result;
import com.geek.factory.service.DeviceRepairLogService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/log")
public class DeviceRepairLogController {


    @Resource
    private DeviceRepairLogService deviceRepairLogService;

    // 查询列表（含产线名）
    @GetMapping("/list")
    public Result list() {
        List<DeviceRepairLogDTO> list = deviceRepairLogService.getAllWithLineName();
        return new Result(SuccessConstant.SUCCESS, "查询成功", list);
    }

    // 新增
    @PostMapping("/add")
    public Result add(@RequestBody DeviceRepairLog log) {
        boolean saved = deviceRepairLogService.save(log);
        return saved
                ? new Result(SuccessConstant.SUCCESS, "新增成功")
                : new Result(SuccessConstant.FAIL, "新增失败");
    }

    // 修改
    @PutMapping("/update")
    public Result update(@RequestBody DeviceRepairLog log) {
        boolean updated = deviceRepairLogService.updateById(log);
        return updated
                ? new Result(SuccessConstant.SUCCESS, "更新成功")
                : new Result(SuccessConstant.FAIL, "更新失败");
    }

    // 删除
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {
        boolean removed = deviceRepairLogService.removeById(id);
        return removed
                ? new Result(SuccessConstant.SUCCESS, "删除成功")
                : new Result(SuccessConstant.FAIL, "删除失败");
    }

}
